<template>
  <el-row style="height: 100vh">
    <el-col class="left" :xs="8" :md="12" :lg="16">
      <h1>后台管理系统</h1>
      <span>做项目，咱是认真的！</span>
    </el-col>
    <el-col class="right" :xs="16" :md="12" :lg="8">
      <h2>Login</h2>
      <el-form
        ref="formRef"
        :model="formInline"
        :rules="rules"
        class="form-inline"
      >
        <el-form-item prop="username" label="用户名：">
          <el-input v-model="formInline.username" placeholder="请输入用户名">
            <template #prefix>
              <el-icon><User /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password" label="密&nbsp;&nbsp;&nbsp;码：">
          <el-input
            v-model="formInline.password"
            type="password"
            placeholder="请输入密码"
            show-password
          >
            <template #prefix>
              <el-icon><Lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            style="width: 100%"
            @click="onSubmit"
            :loading="isLoading"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>

<script setup>
import { ref, reactive, onMounted, onUnmounted } from "vue";
import { useRouter, useRoute } from "vue-router";
import { login } from "@/api/manage";
import { toast } from "@/utils/index";
import { setCookies } from "@/utils/auth";
import { useAdminStore } from "@/store/admin.js";

const store = useAdminStore();
const isLoading = ref(false);

const router = useRouter();
const formInline = reactive({
  username: "",
  password: "",
});

const rules = reactive({
  username: [
    {
      required: true,
      message: "请输入正确的用户名",
      trigger: "blur",
    },
  ],
  password: [
    {
      required: true,
      message: "请输入正确的密码",
      trigger: "blur",
    },
  ],
});
const formRef = ref(null);

const onSubmit = () => {
  formRef.value.validate((res) => {
    // 需验证后跳转
    isLoading.value = true;
    login(formInline.username, formInline.password)
      .then((res) => {
        // console.log(res, "bingo!");
        //弹出提示框
        toast("登录成功");
        //添加token
        const token = res.token;
        setCookies(token);
        //获取用户信息
        // store.getAdminInfo();
        //路由跳转到首页
        router.push({
          path: "/",
        });
      })
      .catch((err) => {
        console.log(err);
      })
      .finally(() => {
        isLoading.value = false;
      });
  });
};
function keyUp(event) {
  if (event.keyCode === 13) onSubmit();
}

onMounted(() => {
  window.addEventListener("keyup", keyUp);
});
onUnmounted(() => {
  window.removeEventListener("keyup", keyUp);
});
</script>

<style lang="less" scoped>
.left {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background: url("../assets/login-back.jpg") no-repeat 0 0/100% 100%;
  color: #fff;
}
.right {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  // background: pink;
  h2 {
    color: deepskyblue;
  }
}
</style>
